package com.mujin.keji.collectionsystem.module.tripplan.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.tripplan.dto.QueryPlanDetailDto;

import java.util.List;


/**
 * 公交路线规划列表适配器
 *
 * @author weishixiong
 * @Time 2018-04-10
 */

public class SelectPersionAdapter extends RecyclerView.Adapter<SelectPersionAdapter.ViewHolder> {
    private Activity mContext;
    private List<QueryPlanDetailDto> datas;
    private CustomItemClickListener listener;

    public SelectPersionAdapter(Activity context, List<QueryPlanDetailDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_select_person, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        //后面的图标
        if (position > 0) {
            if(datas.get(position).isSelected()){
                holder.tvName.setTextColor(Color.parseColor("#FF5874F1"));
                holder.tvIndex.setTextColor(Color.parseColor("#FFFFFF"));
                holder.tvIndex.setBackgroundResource(R.drawable.image_trip__select);
            }
            else{
                //未结束
                if (datas.get(position).getStatus() != null && datas.get(position).getStatus() == 1) {
                    holder.tvName.setTextColor(Color.parseColor("#FF5874F1"));
                    holder.tvIndex.setTextColor(Color.parseColor("#FF5874F1"));
                    holder.tvIndex.setBackgroundResource(R.drawable.image_trip_no_finish);

                } else {
                    //已经结束
                    holder.tvName.setTextColor(Color.parseColor("#FFBABABA"));
                    holder.tvIndex.setTextColor(Color.parseColor("#FFFFFF"));
                    holder.tvIndex.setBackgroundResource(R.drawable.image_now_noselect);
                }
            }

            holder.llRight.setVisibility(View.VISIBLE);
            holder.tvIndex.setText(String.valueOf(position));
            holder.ivLeft.setVisibility(View.GONE);

        }
        //第一个图标
        else {
            if (datas.get(position).isSelected()) {
                holder.ivLeft.setImageResource(R.drawable.image_trip_my_select);
            } else {
                holder.ivLeft.setImageResource(R.drawable.image_trip_my_normal);
            }
            holder.llRight.setVisibility(View.GONE);
            holder.ivLeft.setVisibility(View.VISIBLE);

        }


        if (position == datas.size() - 1) {
            holder.ivLine.setVisibility(View.INVISIBLE);
        } else {
            holder.ivLine.setVisibility(View.VISIBLE);
        }
        holder.tvName.setText(datas.get(position).getVisitName());
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivLeft;
        private LinearLayout llRight;
        private TextView tvIndex;
        private TextView tvName;
        private View ivLine;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            ivLeft = (ImageView) itemView.findViewById(R.id.iv_left);
            llRight = (LinearLayout) itemView.findViewById(R.id.ll_right);
            tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            ivLine = (View) itemView.findViewById(R.id.iv_line);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }

    }
}
